﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using PersonalFinanceManager.Repositories.Repositories;
using System.Drawing;
using System.Linq.Expressions;

namespace PersonalFinanceManager.Website
{
    public partial class BillTypes : System.Web.UI.Page
    {
        BillsRepository billsRepo = new BillsRepository();
        bool DoSearch = false;
        int sessionUserID;

        protected void Page_Load(object sender, EventArgs e)
        {
            sessionUserID = 1; // Convert.ToInt32(Session["UserID"]);

            if (!IsPostBack)
            {
                btnUpdate.Visible = false;
            }
        }

        protected void Page_PreRender(object sender, EventArgs e)
        {
            IList<DataModel.Bills> bills;

            if (DoSearch)
            {
                string s = txtSearch.Text;
                Expression<Func<DataModel.Bills, bool>> whereCondition = (x => x.Name.Contains(s));
                bills = billsRepo.FindAll(whereCondition, (x => x.Name), Repositories.SortingMode.ascending);
            }
            else
                bills = billsRepo.GetAll((x => x.Name), Repositories.SortingMode.ascending);

            gvBills.DataSource = bills;
            gvBills.DataBind();

            lblTotalCount.Text = bills.Count.ToString();
        }

        protected void gvBills_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int id = Convert.ToInt32(e.Keys["ID"]);
            DataModel.Bills bill = new DataModel.Bills();
            bill.ID = id;

            billsRepo.Delete(bill);
        }

        protected void gvBills_SelectedIndexChanged(object sender, EventArgs e)
        {
            int id = Convert.ToInt32(gvBills.DataKeys[gvBills.SelectedIndex]["ID"]);
            DataModel.Bills bill = billsRepo.GetSingle(x => x.ID == id);
            ViewState["ID"] = bill.ID;

            txtName.Text = bill.Name;

            ResetForm(false, bill.Name);
        }

        protected void btnInsert_Click(object sender, EventArgs e)
        {
            DataModel.Bills bill = new DataModel.Bills();
            bill.Name = txtName.Text;
            bill.UserID = sessionUserID;

            billsRepo.Insert(bill);

            ResetForm(true, "");
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            int id = Convert.ToInt32(ViewState["ID"]);
            DataModel.Bills bill = billsRepo.GetSingle(x => x.ID == id);
            bill.Name = txtName.Text;

            billsRepo.Update(bill);

            ResetForm(true, "");
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            ResetForm(true, "");
        }

        protected void ResetForm(bool isInsert, string billname)
        {
            if (isInsert)
            {
                btnInsert.Visible = true;
                btnUpdate.Visible = false;
                pnlBills.GroupingText = "Внеси потрошувачка сметка";
                pnlBills.DefaultButton = "btnInsert";

                foreach (Control c in pnlBills.Controls)
                    if (c is TextBox)
                        (c as TextBox).Text = "";
            }
            else
            {
                btnInsert.Visible = false;
                btnUpdate.Visible = true;
                pnlBills.GroupingText = "Промени сметка " + billname;
                pnlBills.DefaultButton = "btnUpdate";
            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            DoSearch = true;
        }
    }
}